package com.gitee.wsl.mathematics.geometry.d2.curve.equation

class ThreeEquationSystemSolver {
    // | a1     b1      c1 |         | firstVariable  |        | d1 |
    // | a2     b2      c2 |    *    | secondVariable |    =   | d2 |
    // | a3     b3      c3 |         | thirdVariable  |        | d3 |
    private var a1 = 0.0
    private var a2 = 0.0
    private var a3 = 0.0
    private var b1 = 0.0
    private var b2 = 0.0
    private var b3 = 0.0
    private var c1 = 0.0
    private var c2 = 0.0
    private var c3 = 0.0
    private var d1 = 0.0
    private var d2 = 0.0
    private var d3 = 0.0
    var firstVariable: Double = 0.0
        private set
    var secondVariable: Double = 0.0
        private set
    var thirdVariable: Double = 0.0
        private set


    fun setFirstEquation(a: Double, b: Double, c: Double, d: Double) {
        a1 = a
        b1 = b
        c1 = c
        d1 = d
    }


    fun setSecondEquation(a: Double, b: Double, c: Double, d: Double) {
        a2 = a
        b2 = b
        c2 = c
        d2 = d
    }


    fun setThirdEquation(a: Double, b: Double, c: Double, d: Double) {
        a3 = a
        b3 = b
        c3 = c
        d3 = d
    }


    /**
     * From Cramer's Rule:
     *
     *
     *
     * | a1     b1      c1 |
     * Delta =                  | a2     b2      c2 |
     * | a3     b3      c3 |
     *
     *
     * | d1     b1      c1 |
     * FirstVariable =          | d2     b2      c2 |   ÷   Delta
     * | d3     b3      c3 |
     *
     *
     * | a1     d1      c1 |
     * SecondVariable =         | a2     d2      c2 |   ÷   Delta
     * | a3     d3      c3 |
     *
     *
     * | a1     b1      d1 |
     * ThirstVariable =         | a2     b2      d2 |   ÷   Delta
     * | a3     b3      d3 |
     *
     */
    fun solve() {
        val delta = a1 * (b2 * c3 - b3 * c2) - b1 * (a2 * c3 - a3 * c2) + c1 * (a2 * b3 - a3 * b2)

        firstVariable =
            (d1 * (b2 * c3 - b3 * c2) - b1 * (d2 * c3 - d3 * c2) + c1 * (d2 * b3 - d3 * b2)) / delta

        secondVariable =
            (a1 * (d2 * c3 - d3 * c2) - d1 * (a2 * c3 - a3 * c2) + c1 * (a2 * d3 - a3 * d2)) / delta

        thirdVariable =
            (a1 * (b2 * d3 - b3 * d2) - b1 * (a2 * d3 - a3 * d2) + d1 * (a2 * b3 - a3 * b2)) / delta
    }
}
